package top.chmin.project.cloud.sys.relate.info;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.Page;
import top.chmin.project.cloud.sys.column.info.ColumnInfo;
import top.chmin.project.cloud.sys.column.info.ColumnInfoCondition;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



/**
 * chmin
 * 2024-01-26 14:02:31
 * cloud-code
 */
@Service
@Transactional
public class RelateInfoService {

    @Resource
    RelateInfoPlugin plugin;

    public int save(RelateInfoSaveRest.P p) {
        plugin.checkSave(p);
        RelateInfo relateInfo = BeanUtil.toBean(p, RelateInfo.class);
        plugin.beforeSave(relateInfo);
        int result = relateInfo.forInsert().execute();
        plugin.afterSave(p, relateInfo, result);
        return result;
    }

    public RelateInfoSelectRest.R select(RelateInfoSelectRest.P p) {
        plugin.checkSelect(p);
        RelateInfoCondition c = RelateInfoCondition.forSelect().id().eq(p.getId());
        plugin.beforeSelect(p, c);
        RelateInfoSelectRest.R r = c.selectFirst(RelateInfoSelectRest.R.class);
        plugin.afterSelect(p, r);
        return r;
    }

    public int update(RelateInfoUpdateRest.P p) {
        plugin.checkUpdate(p);
        RelateInfo relateInfo = BeanUtil.toBean(p, RelateInfo.class);
        plugin.beforeUpdate(relateInfo);
        int result = relateInfo.forUpdate().id().eq().execute();
        plugin.afterUpdate(p, relateInfo, result);
        return result;
    }

    public int delete(RelateInfoDeleteRest.P p) {
        plugin.checkDelete(p);
        RelateInfo relateInfo = BeanUtil.toBean(p, RelateInfo.class);
        plugin.beforeDelete(relateInfo);
        int result = relateInfo.forDelete().id().eq().execute();
        plugin.afterDelete(p, relateInfo, result);
        return result;
    }

    public Page<RelateInfoPageRest.R> page(RelateInfoPageRest.P p) {
        plugin.checkPage(p);
        RelateInfoCondition c = RelateInfoCondition.forSelect();
        c = p.isDesc() ? c.id().desc() : c.id().asc();
        plugin.beforePage(p, c);
        Page<RelateInfoPageRest.R> r = c.selectPage(RelateInfoPageRest.R.class);
        /*###########auto#start###########*/
        List<String> columnCodeList = r.getList().stream().map(RelateInfoPageRest.R::getColumnCode).toList();
        List<ColumnInfo> columnInfoList = ColumnInfoCondition.forSelect().columnCode().in(columnCodeList).selectList();
        Map<String, ColumnInfo> columnInfoMap = columnInfoList.stream().collect(Collectors.toMap(ColumnInfo::getColumnCode, t -> t));
        r.getList().forEach(t -> {
            ColumnInfo columnInfo = columnInfoMap.get(t.getColumnCode());
            t.setColumnCodeText(columnInfo.getColumnName());

        });

        List<String> sourceColumnCodeList = r.getList().stream().map(RelateInfoPageRest.R::getSourceColumnCode).toList();
        List<ColumnInfo> sourceColumnInfoList = ColumnInfoCondition.forSelect().columnCode().in(sourceColumnCodeList).selectList();
        Map<String, ColumnInfo> sourceColumnInfoMap = sourceColumnInfoList.stream().collect(Collectors.toMap(ColumnInfo::getColumnCode, t -> t));
        r.getList().forEach(t -> {
            ColumnInfo columnInfo = sourceColumnInfoMap.get(t.getSourceColumnCode());
            t.setSourceColumnCodeText(columnInfo.getColumnName());

        });

        /*###########auto##end############*/
        /* append $serviceRelateAppender$ */
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceDictAppender$ */
        plugin.afterPage(p, r);
        return r;
    }
    /* append $serviceSelectOptionMethodAppender$ */
}
